<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class OrderAction extends HomeAction {

    public function Index(){
        $payStatus = I('payStatus');
        if ($payStatus!='') {
            $map['payStatus']=$payStatus;
        }
        $obj = M('Order');
        $map['memberID'] = $this->_user['memberID'];
        $map['del'] = 0;
        $count = $obj->where($map)->count();
        import("@.ORG.WapPage");
        $page = new Page($count, 5);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $where['order_no'] = $value['order_no'];
            $list[$key]['shop'] = M('OrderDetail')->field('id,pid,picname,pname,number,price,comment')->where($where)->select();
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('payStatus', $payStatus);
        $this->display();
    }

    public function cancel(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['memberID'] = $this->_user['memberID'];
        $map['del'] = 0;
        //$map['payStatus'] = 0;
        M('Order')->where($map)->setField('del',1);
        redirect ($_SERVER['HTTP_REFERER']);
    }

    public function view(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['del'] = 0;
        $map['memberID'] = $this->_user['memberID'];
        $list = M('Order')->where($map)->find();
        if ($list) {
            $where['order_no'] = $list['order_no'];
            $list['shop']=M('OrderDetail')->where($where)->select();
            $this->assign('list',$list);
            $this->display();
        }else{
            $this->error("没有该订单");
        }        
    }

    public function comment(){
        $id = I('get.id');
        $map['id']= $id;
        $list = M('OrderDetail')->where($map)->find();
        if ($list) {
            if ($list['comment']==1) {
                $this->error("不能重复点评");
            }else{
                $this->assign('list',$list);
                $this->display();
            }            
        }else{
            $this->error("信息不存在");
        }        
    }

    public function savecomm(){
        $obj = D('Comment');
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $state = 'SUCCESS';
                $message = '操作成功';
                $url = U('Order/index');

                M('OrderDetail')->where('id='.I('post.detailID'))->setField('comment','1');
            } else {
                $state = '操作失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    //物流查询
    public function result(){
        /*
        http://www.jisuapi.com/my/
        */
        $type = 'zhongtong';
        $postid = I('get.postid');
        $url = 'http://www.kuaidi100.com/query?type='.$type.'&postid='.$postid.'&id=1&valicode=&temp='.I('post.temp');       
        $url = 'http://api.jisuapi.com/express/query?appkey=1c4a6ee6e0b0bb2b&type=auto&number='.$postid;
        //$url = 'http://api.kuaidi100.com/api?id=c11b40a12b4c21e7&com='.$type.'&nu='.$postid.'&show=2&muti=&order=';
        //902019591351 、ev945980925cn
        //echo $url;
        $con = file_get_contents($url);
        $con = json_decode($con);
        $message = $con->msg; 
        $msg = '';
        if ($message=='ok') {
            $data = $con->result->list;
            foreach(array_reverse($data) as $i=>$v){
                $msg .= '<li><span>'.$v->time.'</span><p>'.$v->status.'</p></li>';
            }
        }else{
            $msg = '<li class="error">此单号无记录，请核对快递公司名称和运单号码是否正确！</li>';
        }
        $this->assign('msg',$msg);
        $this->assign('postid',$postid);
        $this->display();
    }
}